package la.bbs558.util;

import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.ReflectUtil;
import la.bbs558.entity.enums.UserMessageType;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class EnumUtils extends EnumUtil {

    private static final Map<Class<?>,List<Map<String,String>>> ENUM_MAP = new ConcurrentHashMap<>();

    public static <E extends Enum<E>> List<Map<String,String>> toList(Class<E> enumClass, String label, String value) {
        List<Map<String, String>> list = ENUM_MAP.get(enumClass);
        if(Objs.isValid(list)){
            return list;
        }
        LinkedHashMap<String, E> enumMap = getEnumMap(enumClass);
        Collection<E> values = enumMap.values();
        list = new ArrayList<>();
        for (E e : values) {
            list.add(new HashMap<String,String>(){{
                Object labelVal = ReflectUtil.getFieldValue(e, label);
                put("label", Objs.getOrElse(labelVal,"").toString());
                Object valueVal = ReflectUtil.getFieldValue(e, value);
                put("value", Objs.getOrElse(valueVal,"").toString());
            }});
        }
        ENUM_MAP.put(enumClass,list);
        return list;
    }

    public static void main(String[] args) {
        List<Map<String, String>> list = toList(UserMessageType.class,"desc","code");
        System.out.println(list);
    }
}
